<?php


namespace App\Http\Controllers\Admin;
use App\Http\Controllers\BaseController;
use App\Models\Search;
use Illuminate\Http\Request;

class SearchController extends BaseController
{
    /**
     * 列表
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request) {
        $input = $request->input();
        $search = ['start_at' => '', 'end_at' => ''];
        $keyword = $request->input('keyword');

        $list = Search::apply($search, $input)
            ->when($keyword, function ($q) use($keyword) {
                $q->where('keyword', 'like', '%' . $keyword . '%');
            })
            ->withCertain('user', ['nickname'])
            ->orderBy("id", "desc")
            ->paginate(per_page());
        return $this->paginate($list);
    }

    public function statistics(Request $request) {
        $input = $request->input();
        $search = ['start_at' => '', 'end_at' => ''];
        $keyword = $request->input('keyword');
        $list = Search::apply($search, $input)  ->when($keyword, function ($q) use($keyword) {
            $q->where('keyword', 'like', '%' . $keyword . '%');
        })->groupBy('keyword')->selectRaw('keyword,count(id) as total')->orderBy('total', 'desc')->paginate(per_page());
        return $this->paginate($list);
    }
}

